package com.liuym.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.serializer.ValueFilter;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * FastJson工具类
 *
 * @author Liuym
 * @date 2019/1/21 14:15
 */
public class FastJsonUtil {
    /**
     * The mapping.
     */
    private static SerializeConfig mapping = new SerializeConfig();

    /**
     * 过滤器.
     */
    private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if ("detail".equals(s) && v != null) {//详情
                String str = v.toString();
                if (str.length() > 10) {
                    return str.substring(0, 10) + "...";
                }
            }
            return v;
        }
    };

    static {
        mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * javaBean、list、map convert to json string.
     *
     * @param obj the obj
     * @return the string
     */
    public static String obj2jsonOfFilter(Object obj) {
        return JSON.toJSONString(obj, mapping, filter, new SerializerFeature[]{});
    }

    /**
     * Obj 2 json fomate date.
     *
     * @param obj the obj
     * @return the string
     */
    public static String obj2jsonFomateDate(Object obj) {
        return JSON.toJSONString(obj, mapping, new SerializerFeature[]{});
    }

    /**
     * Obj 2 json.
     *
     * @param obj the obj
     * @return the string
     */
    public static String obj2json(Object obj) {
        return JSON.toJSONString(obj);
    }

    /**
     * 转换json时不处理特殊字符.
     *
     * @param obj the obj
     * @return the string
     */
    public static String obj2jsonDisableCheckSpecialChar(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.DisableCheckSpecialChar);
    }

    /**
     * json string convert to javaBean、map.
     *
     * @param <T>     the generic type
     * @param jsonStr the json str
     * @param clazz   the clazz
     * @return the t
     */
    public static <T> T json2obj(String jsonStr, Class<T> clazz) {
        return JSON.parseObject(jsonStr, clazz);
    }

    /**
     * json array string convert to list with javaBean.
     *
     * @param <T>          the generic type
     * @param jsonArrayStr the json array str
     * @param clazz        the clazz
     * @return the list
     */
    public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) {
        return JSON.parseArray(jsonArrayStr, clazz);
    }

    /**
     * json string convert to map.
     *
     * @param jsonStr the json str
     * @return the map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> json2map(String jsonStr) {
        return json2obj(jsonStr, Map.class);
    }

    /**
     * json string convert to map with javaBean.
     *
     * @param <T>     the generic type
     * @param jsonStr the json str
     * @param clazz   the clazz
     * @return the map
     */
    public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) {
        Map<String, T> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, T>>() {
        });
        for (Entry<String, T> entry : map.entrySet()) {
            JSONObject obj = (JSONObject) entry.getValue();
            map.put(entry.getKey(), JSONObject.toJavaObject(obj, clazz));
        }
        return map;
    }

}
